<?php

namespace app\services;

use app\payment\PayFactory;
use EasyWeChat\Factory;
use plugin\admin\app\model\OrderGoods;
use plugin\admin\app\model\User;
use support\EasyWechatFactory;
use Webman\Config;

class WechatMiniProgramShippingService
{
    /**
     * 小程序发货
     *
     * @param $order
     * @return void
     * @throws \Exception
     */
    public static function wechatOrder($order)
    {
        if ($order['pay_way'] != PayFactory::WECHAT || $order['pay_way_type'] != 'mini') {
            return;
        }

        $orderGoods = OrderGoods::where('order_no', $order['order_no'])->first();

        $date = new \DateTime();
        $rfc3339 = $date->format(\DateTime::RFC3339);

        $mchId = ConfigService::get('mch_id');
        $data = [
            'order_key' => [
                'order_number_type' => 1,
                'mchid' => $mchId,
                'out_trade_no' => $order['order_no']
            ],
            'logistics_type' => 1,
            'delivery_mode' => 1,
            'upload_time' => $rfc3339,
            'shipping_list' => [
                [
                    'tracking_no' => $order['express_no'],
                    'express_company' => $order['express_code'] ?? 'YD',
                    'item_desc' => $orderGoods['goods_name'] ?? '',
                    'contact' => [
                        'receiver_contact' => $order['receive_mobile']
                    ]
                ]
            ],
            'payer' => [
                'openid' => UserService::getOpenId($order)
            ]
        ];

        $app = EasyWechatFactory::getMiniProgramApp();
        $app->shipping->uploadShippingInfo($data);
    }

    public static function wechatOrderNotShipping($order)
    {
        if ($order['pay_way'] != PayFactory::WECHAT || $order['pay_way_type'] != 'mini') {
            return;
        }

        $orderGoods = OrderGoods::where('order_no', $order['order_no'])->first();

        $date = new \DateTime();
        $rfc3339 = $date->format(\DateTime::RFC3339);

        $mchId = ConfigService::get('mch_id');
        $data = [
            'order_key' => [
                'order_number_type' => 1,
                'mchid' => $mchId,
                'out_trade_no' => $order['order_no']
            ],
            'logistics_type' => 3,
            'delivery_mode' => 1,
            'upload_time' => $rfc3339,
            'shipping_list' => [
                [
                    'item_desc' => '充值',
                ]
            ],
            'payer' => [
                'openid' => UserService::getOpenId($order)
            ]
        ];

        $app = EasyWechatFactory::getMiniProgramApp();
        return $app->shipping->uploadShippingInfo($data);
    }

    /**
     * 小程序提醒收货
     *
     * @param $order
     * @return void
     * @throws \Exception
     */
    public static function wechatNotifyReceive($order)
    {
        if ($order['pay_way'] != PayFactory::WECHAT || $order['pay_way_type'] != 'mini') {
            return;
        }

        $data = [
            'merchant_trade_no' => $order['order_no'],
            'received_time' => time()
        ];

        $app = EasyWechatFactory::getMiniProgramApp();
        $app->shipping->uploadShippingInfo($data);
    }

    public static function submitPath($path)
    {
        $app = EasyWechatFactory::getMiniProgramApp();
        $app->shipping->setMsgJumpPath($path);
    }
}